/*
 * @Description: 
 * @Author: lgt
 * @Date: 2021-06-26 15:15:35
 * @LastEditTime: 2021-07-10 23:20:28
 * @LastEditors: xxx
 */
const Router = require('koa-router');
const router = new Router({
    prefix:'/v1/classic'
}
);

//导入token校验
const {Auth} = require("../../../middlewares/auth")
const {Flow} = require("../../models/flow")
const {Movie,Music,Sentence} = require('../../models/classic')
const {Art} = require("../../models/art");
const {Favor} = require("../../models/favor")

//导入校验器
const {PositiveIntegerValidator, ClassicValidator} = require('../../validators/classic')

router.get('/latest-getparams',new Auth().m,async (ctx,next)=>{
    
    const path = ctx.params; //取得动态路由参数
    const query = ctx.request.query; //获取查询参数
    const headers = ctx.request.header;//获取请求头数据
    const body = ctx.request.body;//获取post请求中的 body 数据，需要借助koa-bodyparser

    // const v =await new PositiveIntegerValidator().validate(ctx);//传递校验参数 执行上下文
    //校验器 获取http请求参数
    // const id = v.get('path.id');
    const uid = ctx.auth.uid;

    ctx.body = {
        path,
        query,
        headers,
        body,
        uid
    }

})


router.get('/latest', new Auth().m, async (ctx, next) => {
    const flow = await Flow.findOne({
        order: [
            ['index', 'DESC']
        ]
    })

    let art = await Art.getData(flow.art_id, flow.type);
    const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid);

    art.setDataValue('index', flow.index);
    art.setDataValue('like_status', likeLatest);

    ctx.body = {
        art
    }
})

router.get('/:index/next', new Auth().m, async (ctx, next) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })

    const index = v.get('path.index');
    const flow = await Flow.findOne({
        where: {
            index: index + 1
        }
    });
    if (!flow) {
        throw  new global.errs.NotFound();
    }

    let art = await Art.getData(flow.art_id, flow.type);
    const likeNext = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid);

    art.setDataValue('index', flow.index);
    art.setDataValue('like_status', likeNext);

    ctx.body = {
        art
    }
})


router.get('/:index/previous', new Auth().m, async (ctx, next) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })

    const index = v.get('path.index');
    const flow = await Flow.findOne({
        where: {
            index: index - 1
        }
    });
    if (!flow) {
        throw  new global.errs.NotFound();
    }

    let art = await Art.getData(flow.art_id, flow.type);
    const likePrevious = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid);

    art.setDataValue('index', flow.index);
    art.setDataValue('like_status', likePrevious);

    ctx.body = {
        art
    }
})

router.get('/:type/:id', new Auth().m, async ctx=>{
    const v = await new ClassicValidator().validate(ctx)
    const id = v.get('path.id')
    const type = parseInt(v.get('path.type'))

    const artDetail =await new Art(id,type).getDetail(ctx.auth.uid)

    artDetail.art.setDataValue('like_status', artDetail.like_status)
    ctx.body = artDetail.art
})

// 获取报刊点赞数量
// 获取报刊点赞数量
router.get('/:type/:id/favor', new Auth().m, async ctx => {
    const v = await new ClassicValidator().validate(ctx);
    const id = v.get('path.id')
    const type = parseInt(v.get('path.type'));

    const art = await Art.getData(id, type);
    if (!art) {
        throw new global.errs.NotFound();
    }

    const like = await Favor.userLikeIt(
        id, type, ctx.auth.uid
    )
    ctx.body = {
        fav_nums: art.fav_nums,
        like_status: like
    }
})

router.get('/favor', new Auth().m, async ctx => {
    const uid = ctx.auth.uid
    ctx.body = await Favor.getMyClassicFavors(uid)
})



module.exports = router;